package com.by.util;

import com.by.model.SecurityUser;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Set;

/**
 * @author ServivE
 * @version 0.1
 * @className AuthUtils
 * @description 授权工具类
 * @date 2024/12/9 15:14
 * @since jdk17
 */
public class AuthUtils {
    /**
     * 获取Security容器中的认证用户对象
     * @return
     */
    public static SecurityUser getLoginUser() {
        return (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /**
     * 获取登录用户ID
     * @return
     */
    public static Long getLoginUserId() {
        return getLoginUser().getUserId();
    }

    /**
     * 获取登录用户的权限集合
     * @return
     */
    public static Set<String> getPerms() {
        return getLoginUser().getPerms();
    }
}

